import { getUrlQuery } from '@/utils'

/**
 * 根据渠道号动态加载字体
 * pf开头的渠道使用SourceHanSansCN字体，其他渠道使用Source_Han_Sans_SC字体
 */
export function initFont() {
  const { channelCode } = getUrlQuery()

  // 创建style元素
  const styleElement = document.createElement('style')
  styleElement.type = 'text/css'

  // 根据渠道号选择不同的字体
  if (channelCode && channelCode.toString().startsWith('pf')) {
    // pf开头的渠道使用SourceHanSansCN字体
    styleElement.textContent = `
      @font-face {
        font-family: 'MyCustomFont';
        src: url('https://fst1.fuxiaoli.com/fonts/SourceHanSansCN-Bold.otf') format('opentype');
        font-weight: bold;
        font-style: normal;
      }

      @font-face {
        font-family: 'MyCustomFont';
        src: url('https://fst1.fuxiaoli.com/fonts/SourceHanSansCN-Medium.otf') format('opentype');
        font-weight: 500;
        font-style: normal;
      }

      @font-face {
        font-family: 'MyCustomFont';
        src: url('https://fst1.fuxiaoli.com/fonts/SourceHanSansCN-Regular.otf') format('opentype');
        font-weight: normal;
        font-style: normal;
      }
    `
  } else {
    // 其他渠道使用Source_Han_Sans_SC字体
    styleElement.textContent = `
      @font-face {
        font-family: 'MyCustomFont';
        src: url('https://fst1.fuxiaoli.com/fonts/Source_Han_Sans_SC_Bold.otf') format('opentype');
        font-weight: bold;
        font-style: normal;
      }

      @font-face {
        font-family: 'MyCustomFont';
        src: url('https://fst1.fuxiaoli.com/fonts/Source_Han_Sans_SC_Medium_Medium.otf') format('opentype');
        font-weight: 500;
        font-style: normal;
      }

      @font-face {
        font-family: 'MyCustomFont';
        src: url('https://fst1.fuxiaoli.com/fonts/Source_Han_Sans_SC_Regular.otf') format('opentype');
        font-weight: normal;
        font-style: normal;
      }
    `
  }

  // 将style元素添加到文档头部
  document.head.appendChild(styleElement)

  // 设置全局字体样式
  document.documentElement.style.fontFamily =
    'MyCustomFont, PingFangSC-Regular, PingFang SC, Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif'
}
